from django.shortcuts import render
from rest_framework.response import Response
from . import models
from .Ser import *
# Create your views here.
from rest_framework.views import APIView
class LoginView(APIView):
    def post(self,request):
        username=request.POST.get("username")
        password=request.POST.get("password")

        userobj=models.Login.objects.filter(username=username).first()

        if userobj:
            return Response({"code":1001,"msg":"数据已存在"})
        data={
            'username':username,
            'password':password,
        }
        obj=Userserializers(data=data)
        if obj.is_valid():
            flay=obj.save()
            if flay:
                return Response({"code":1000,"msg":"创建成功"})
            return Response({"code":1002,"msg":"创建失败"})

        # print(obj.errors)
        return Response({"code":1003,"msg":"参数错误"})
class User(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        obj = models.Login.objects.filter(username = username,password = password)
        # print(obj)
        if obj:
            data = {
                "userid": obj.id
            }
            return Response({'msg':'登录成功','code':200,'data':data})
        return Response({'msg':'登陆失败','code':400,'data':''})

# class LoginView(APIView):
#     def post(self,request):
#         username = request.data.get('username')
#         password = request.data.get('password')
#         obj =



